﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using log4net;

namespace DataLayer.SQL
{
    public class DataAccess
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(DataAccess));
        
        public string ConnectionString { 
            get {
                return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            }
        }

        /// <summary>
        /// Obtiene la coleccion de datos representada en un DataTable a partir de un CommandSQL.
        /// </summary>
        /// <param name="commandSQL">Clase que representa el comando a ejecutar.</param>
        /// <returns>Colección de datos obtenidos de una consulta SQL.</returns>
        public DataTable GetDataTable(SqlCommand command)
        {
            DataTable table = new DataTable();
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                try
                {
                    command.Connection = connection;
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    connection.Open();
                    adapter.Fill(table);
                }
                catch (Exception exc)
                {
                    logger.Error(exc.Message, exc);
                    throw exc;
                }
            }
            return table;
        }
    }
}
